package android.test.devicetest;

import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.telephony.TelephonyManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.test.until.Max3HWVerion;

public class GprsReceiver extends BroadcastReceiver {
    private final String   TAG = "GprsReceiver";
    private final String   GPRS_CARRIERID_FILE = "gprs_carrier_id";
    private final String[] CARRIER_LIST = new String[] {
        "46000",
        "46001",
        "46002",
        "46003",
        "46007",
        "45400",
        "45402",
        "45403",
        "45404",
        "45410",
        "45412",
        "45418",
    };

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager    tm = (TelephonyManager   )context.getSystemService(Context.TELEPHONY_SERVICE   );
        NetworkInfo    network = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (network.isConnected())
        {
            String strCarrierIdCur  = tm.getNetworkOperator();
            String strCarrierIdLast = null;

            try {
                byte[] buf = new byte[16];
                int    len = 0;
                FileInputStream is = context.openFileInput(GPRS_CARRIERID_FILE);
                len = is.read(buf);
                is.close();
                strCarrierIdLast = new String(buf, 0, len);
            } catch (Exception e) {}
                
            Log.d(TAG, "strCarrierIdCur = " + strCarrierIdCur + " strCarrierIdLast = " + strCarrierIdLast);

            if (!strCarrierIdCur.equals(strCarrierIdLast))
            {
                try {
                    FileOutputStream os = context.openFileOutput(GPRS_CARRIERID_FILE, Context.MODE_PRIVATE);
                    os.write(tm.getNetworkOperator().getBytes());
                    os.flush();
                    os.close();
                } catch (Exception e) {}

                if (strCarrierIdLast != null)
                {
                    if (!isInTheSameArea(strCarrierIdLast, strCarrierIdCur))
                    {
                        Log.d(TAG, "current id and last id are not in the same area, need reset gps data !");
                        Max3HWVerion.runRootCommand("rm -rf /data/gps/*");
                        Max3HWVerion.runRootCommand("killall -9 gpsexe");
                        Max3HWVerion.runRootCommand("/system/bin/gps.sh -nrealtime");
                    }
                }
            }
        }
    }
    
    private boolean isInTheSameArea(String id1, String id2)
    {
        boolean flag1 = false;
        boolean flag2 = false;
        for (String cur : CARRIER_LIST)
        {
            if (cur.equals(id1)) flag1 = true;
            if (cur.equals(id2)) flag2 = true;
        }
        return (flag1 == flag2);
    }
}

